package com.zhangyu.shadow.ec.sign;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.view.View;

import com.zhangyu.shadow.core.delegate.ShadowDelegate;
import com.zhangyu.shadow.core.net.RestClient;
import com.zhangyu.shadow.core.net.callback.IError;
import com.zhangyu.shadow.core.net.callback.IFailure;
import com.zhangyu.shadow.core.net.callback.ISuccess;
import com.zhangyu.shadow.core.util.regex.RegexUtils;
import com.zhangyu.shadow.core.weixin.ShadowWeChat;
import com.zhangyu.shadow.core.weixin.callbacks.IWeChatSignInCallback;
import com.zhangyu.shadow.ec.R;
import com.zhangyu.shadow.ec.R2;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author zhangyu
 * @date 2017/10/25
 */

public class SignInDelegate extends ShadowDelegate {
    private static final String TAG = "SignInDelegate";
    @BindView(R2.id.et_account)
    TextInputEditText mEtAccount;
    @BindView(R2.id.et_password)
    TextInputEditText mEtPassword;
    private ISignListener mISignListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ISignListener) {
            mISignListener = (ISignListener) activity;
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_sign_in;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

    }

    private boolean checkForm() {
        String phone = mEtAccount.getText().toString();
        String password = mEtPassword.getText().toString();

        boolean isPass = true;

        if (!RegexUtils.isMobileExact(phone)) {
            mEtAccount.setError("错误的手机号码");
            isPass = false;
        } else {
            mEtAccount.setError(null);
        }

        if (password.isEmpty() || password.length() < 6) {
            mEtPassword.setError("密码不能小于6位");
            isPass = false;
        } else {
            mEtPassword.setError(null);
        }

        return isPass;
    }

    @OnClick(R2.id.btn_sign_in)
    public void onLogin() {
        if (checkForm()) {
            RestClient.builder().url("user_profile.php")
                    .loader(getContext())
                    .params("","")
                    .success(new ISuccess() {
                        @Override
                        public void onSuccess(String response) {
                            SignHandler.onSignIn(response, mISignListener);
                        }
                    })
                    .failure(new IFailure() {
                        @Override
                        public void onFailure() {

                        }
                    })
                    .error(new IError() {
                        @Override
                        public void onError(int code, String msg) {

                        }
                    })
                    .build()
                    .post();
        }
    }

    @OnClick(R2.id.tv_link_sign_up)
    public void onSignUp() {
        start(new SignUpDelegate(), SINGLETASK);
    }

    @OnClick(R2.id.icon_sign_in_we_chat)
    public void onWeChatLogin() {
        ShadowWeChat.getInstance().onSignInCallback(new IWeChatSignInCallback() {
            @Override
            public void onSignInSuccess(String userInfo) {

            }
        }).signIn();
    }
}
